<?php
// +----------------------------------------------------------------------
// | Quotes [ 只为给用户更好的体验]**[我知道发出来有人会盗用,但请您留版权]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青涩  <3489621943@qq.com>          盗用不留版权,你就不配拿去!
// +----------------------------------------------------------------------
// | Date: 2021年03月24日
// +----------------------------------------------------------------------

    include("../includes/common.php");
    
    if($_REQUEST['redirect_uri']){
        $arr = parse_url($_REQUEST['redirect_uri']);
        if($arr['host']){
            $host = $arr['host'];
            
            $app = $DB->getRow("SELECT * FROM ucenter_apps WHERE url='{$host}' limit 1");
            
               
            if($app){
                 $url = 'http://'.$_SERVER['HTTP_HOST'].'/connect.php?'.http_build_query([
                     'appid'=>$app['appid'],
                     'appkey'=>$app['appkey'],
                     'type'=>'qq',
                     'act'=>'login',
                     'redirect_uri'=>$_REQUEST['redirect_uri'],
                     'state'=>$_REQUEST['state'],
                ]);
                $data = get_curl($url);
                //state 和appid存放授权记录
                $data = json_decode($data,true);
                header('Location:'.$data['url']);
                return;
            }
        }
        echo '不存在的授权';
    }else if($_REQUEST['key']=='zero2109877665'){
        $state = $_REQUEST['state'];
        $code = $_REQUEST['code'];
        $info = $DB->getRow("SELECT * FROM ucenter_logs WHERE state='{$state}' and  code='{$code}'  limit 1");
        $appid = $info['appid'];
        $app = $DB->getRow("SELECT * FROM ucenter_apps WHERE appid='{$appid}' limit 1");
        //根据code  state获取是哪个应用
        $url = 'http://'.$_SERVER['SERVER_NAME'].'/connect.php?'.http_build_query([
             'appid'=>$app['appid'],
             'appkey'=>$app['appkey'],
             'type'=>'qq',
             'act'=>'callback',
             'code'=>$_REQUEST['code']
        ]);
        echo get_curl($url);
    }
    ?>